package com.dimanche.library_db.repository;

import androidx.lifecycle.LiveData;

import com.dimanche.library_db.AppDatabase;
import com.dimanche.library_db.entity.UserEntity;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * author : Dimanche
 * date   : 2021/3/9 16:01
 * desc   :
 * version: 1.0
 */
public class UserRepository {

    //=======================插入================
    public Observable<Long> insert(UserEntity t) {
        return Observable.create((ObservableOnSubscribe<Long>) emitter -> {
            Long insert = AppDatabase.getDatabase().getUserDao().insert(t);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<Long>> insert(UserEntity... userEntities) {
        return Observable.create((ObservableOnSubscribe<List<Long>>) emitter -> {
            List<Long> insert = AppDatabase.getDatabase().getUserDao().insert(userEntities);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<Long>> insert(List<UserEntity> userEntityList) {
        return Observable.create((ObservableOnSubscribe<List<Long>>) emitter -> {
            List<Long> insert = AppDatabase.getDatabase().getUserDao().insert(userEntityList);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

    }


    //==========================查询=======================
    public LiveData<List<UserEntity>> queryAll() {
        return AppDatabase.getDatabase().getUserDao().queryAll();
    }

    public LiveData<UserEntity> queryById(int id) {
        return AppDatabase.getDatabase().getUserDao().queryById(id);
    }

    public LiveData<List<UserEntity>> queryByName(String name) {
        return AppDatabase.getDatabase().getUserDao().queryByName(name);
    }


}
